package com.lovo.util;

import java.io.*;

public class FileUtil {
    //序列化方法
    public static void writeObject(String path,Object obj){
        FileOutputStream fos=null;
        ObjectOutputStream oos =null;

        try {
            fos = new FileOutputStream(path);
            oos =new ObjectOutputStream(fos);
            oos.writeObject(obj);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (fos !=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //反序列化方法
    public static Object rederObject(String path){
        FileInputStream fis =null;
        ObjectInputStream ois =null;
        Object obj=null;

        try {
            fis =new FileInputStream(path);
            ois =new ObjectInputStream(fis);
            return obj= ois.readObject();


        } catch (FileNotFoundException e) {
            e.printStackTrace();

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally {
            if (fis!= null){
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    return null;
    }
}
